package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.mapper.AppointmentMapper;
import cn.wolfcode.car.business.query.AppointmentQuery;
import cn.wolfcode.car.business.service.IAppointmentService;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BaseException;
import com.github.pagehelper.PageHelper;
import org.springframework.aop.ThrowsAdvice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class AppointmentServiceImpl implements IAppointmentService {

    @Autowired
    private AppointmentMapper appointmentMapper;
    @Autowired
    private IStatementService statementService;


    @Override
    public TablePageInfo<Appointment> query(AppointmentQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<Appointment>(appointmentMapper.selectForList(qo));
    }


    @Override
    public void save(Appointment appointment) {
        Appointment oldObj = new Appointment();
        oldObj.setCustomerName(appointment.getCustomerName());
        oldObj.setCustomerPhone(appointment.getCustomerPhone());
        oldObj.setAppointmentTime(appointment.getAppointmentTime());
        oldObj.setLicensePlate(appointment.getLicensePlate());
        oldObj.setCarSeries(appointment.getCarSeries());
        oldObj.setServiceType(appointment.getServiceType());
        oldObj.setInfo(appointment.getInfo());
        appointmentMapper.insert(oldObj);
    }


    @Override
    public void update(Appointment appointment) {
        Appointment newObj = this.get(appointment.getId());
        if (!Appointment.STATUS_APPOINTMENT.equals(newObj.getStatus())) {
            //如果状态不是预约中,不能修改
            throw new BaseException("非法操作");
        }
        newObj.setCustomerName(appointment.getCustomerName());
        newObj.setCustomerPhone(appointment.getCustomerPhone());
        newObj.setAppointmentTime(appointment.getAppointmentTime());
        newObj.setLicensePlate(appointment.getLicensePlate());
        newObj.setCarSeries(appointment.getCarSeries());
        newObj.setServiceType(appointment.getServiceType());
        newObj.setInfo(appointment.getInfo());

        appointmentMapper.updateByPrimaryKey(newObj);
    }

    @Override
    public void deleteBatch(Long id) {
            appointmentMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<Appointment> list() {
        return appointmentMapper.selectAll();
    }

    @Override
    public Appointment get(Long id) {
        return appointmentMapper.selectByPrimaryKey(id);
    }

    @Override
    public void cancelStatus(Long id, Integer statusCancel) {
         appointmentMapper.cancelStatus(id,statusCancel);
    }

    @Override
    public void arrival(Long id,Integer statusArrival) {
        appointmentMapper.arrivalStatus(id,statusArrival);
    }

    @Override
    @Transactional
    public Statement generateStatement(Long appointmentId) {
        Appointment appointment = this.get(appointmentId);
        //合理性校验
        if (Appointment.STATUS_ARRIVAL.equals(appointment.getStatus())||
                Appointment.STATUS_SETTLE.equals(appointment.getStatus()) ||
                Appointment.STATUS_PAID.equals(appointment.getStatus())) {
            //根据预约单Id查询结果单对象
           Statement statement = statementService.getByAppointmentId(appointmentId);
           if (statement == null) {
               //从预约单中把信息保存到结算单
               statement = new Statement();
               statement.setCustomerName(appointment.getCustomerName());
               statement.setCustomerPhone(appointment.getCustomerPhone());
               statement.setServiceType(appointment.getServiceType());
               statement.setCarSeries(appointment.getCarSeries());
               statement.setAppointmentId(appointmentId);
               statement.setLicensePlate(appointment.getLicensePlate());
               statement.setInfo(appointment.getInfo());
               statement.setActualArrivalTime(appointment.getActualArrivalTime());
               statement.setCreateTime(new Date());
               statementService.saveForAppointment(statement);
               //修改预约单状态(结算单生成)
               appointmentMapper.changeStatus(appointmentId,Appointment.STATUS_SETTLE);
           }
                return statement;
        }else {
         throw new BaseException("非法操作");
        }
    }

    @Override
    public void changeStatus(Long appointmentId, Integer status) {
        appointmentMapper.changeStatus(appointmentId,status);
    }

}

